package org.team4.dao.meta;

import java.util.HashMap;
import java.util.List;

import org.hibernate.SessionFactory;
import org.team4.constants.SQLConstant;
import org.team4.constants.StringConstant;
import org.team4.dao.GenericDAO;
import org.team4.domain.RoleUsers;
import org.team4.domain.ScopeDataVisible;
import org.team4.exception.CRUDException;
import org.team4.util.CacheUtil;
import org.team4.util.Validate;
import org.team4.vo.RoleCache;

import org.team4.dao.meta.ScopeDataVisibleDAO;


public class ScopeDataVisibleDAO extends GenericDAO<ScopeDataVisible> {

	private static final long serialVersionUID = 6128176141458285091L;
	private static ScopeDataVisibleDAO instance;
	
	public static ScopeDataVisibleDAO getInstance(SessionFactory sessionFactory) {
        if(instance == null) {
            instance =  new ScopeDataVisibleDAO(sessionFactory);
        }
        return instance;
    }
	
    /**
     * 构造函数
     * 
     * @param sessionFactory
     */
    private ScopeDataVisibleDAO(SessionFactory sessionFactory) {
        super(sessionFactory, ScopeDataVisible.class);
    }
    
    /**
     * 查找其他用户的可操作权限
     * 
     * @param ruList
     * @return
     * @throws CRUDException
     */
    public HashMap<String, HashMap<String, String>> findOtherUsersOperates(List<RoleUsers> ruList, String userId) throws CRUDException {
    	//从缓存中取出现有权限
        HashMap<String, HashMap<String, String>> operates = new HashMap<String, HashMap<String,String>>();
        RoleCache roleCache = (RoleCache) CacheUtil.get(RoleCache.CACHE_NAME, userId);
        if(roleCache != null) {
        	operates = roleCache.getOtherOperates();
        }
        if (Validate.collectionNotNull(ruList)) {
            for (RoleUsers ru : ruList) {
                List<ScopeDataVisible> sdvList = this.find(SQLConstant.SCOPEDATAVISIBLE_BY_ROLE_HQL, ru.getRole().getId());
                if (Validate.collectionNotNull(sdvList)) {
                    for (ScopeDataVisible sdv : sdvList) {
                        HashMap<String, String> usersOperates = operates.get(sdv.getUsers().getId());
                        if (usersOperates == null) {
                            usersOperates = new HashMap<String, String>();
                            usersOperates.put(sdv.getWebId(), sdv.getVisible());
                        } else {
                        	String usable = usersOperates.get(sdv.getWebId());
                            if (Validate.notNull(usable)) {
                                if (StringConstant.FALSE.equals(usable))
                                    usersOperates.put(sdv.getWebId(), sdv.getVisible());
                            } else {
                                usersOperates.put(sdv.getWebId(), sdv.getVisible());
                            }
                        }
                        operates.put(sdv.getUsers().getId(), usersOperates);
                    }
                }
            }
        }
        return operates;
    }

}